package cn.iocoder.yudao.module.agent.controller.admin.brokerageuser;

import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agent.controller.admin.brokerageuser.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokerageuser.AgentBrokerageUserDO;
import cn.iocoder.yudao.module.agent.convert.brokerageuser.AgentBrokerageUserConvert;
import cn.iocoder.yudao.module.agent.service.brokerageuser.AgentBrokerageUserService;

@Tag(name = "管理后台 - 代理人分销用户")
@RestController
@RequestMapping("/agent/brokerage-user")
@Validated
public class AgentBrokerageUserController {

    @Resource
    private AgentBrokerageUserService brokerageUserService;

    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建代理人分销用户")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:create')")
    public CommonResult<Long> createBrokerageUser(@Valid @RequestBody AgentBrokerageUserCreateReqVO createReqVO) {
        return success(brokerageUserService.createBrokerageUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理人分销用户")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:update')")
    public CommonResult<Boolean> updateBrokerageUser(@Valid @RequestBody AgentBrokerageUserUpdateReqVO updateReqVO) {
        brokerageUserService.updateBrokerageUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理人分销用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:delete')")
    public CommonResult<Boolean> deleteBrokerageUser(@RequestParam("id") Long id) {
        brokerageUserService.deleteBrokerageUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理人分销用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:query')")
    public CommonResult<AgentBrokerageUserRespVO> getBrokerageUser(@RequestParam("id") Long id) {
        AgentBrokerageUserDO brokerageUser = brokerageUserService.getBrokerageUser(id);
        return success(AgentBrokerageUserConvert.INSTANCE.convert(brokerageUser));
    }

    @GetMapping("/list-by-agent")
    @Operation(summary = "获得代理人分销用户列表")
    @Parameter(name = "userId", description = "代理用户编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:query')")
    public CommonResult<List<AgentBrokerageUserRespVO>> getBrokerageUsersByAgentUserId(@RequestParam("userId") Long userId){
        List<AgentBrokerageUserDO> list = brokerageUserService.getBrokerageUsersByAgentUserId(userId);

        // 查询用户信息
        Set<Long> memberIds = convertSet(list, AgentBrokerageUserDO::getUserId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        Set<Long> agentMemberIds = convertSet(list, AgentBrokerageUserDO::getAgentUserId);
        Map<Long, MemberUserRespDTO> agentMemberMap = memberUserApi.getUserMap(agentMemberIds);


        return success(AgentBrokerageUserConvert.INSTANCE.convertList(list, memberMap, agentMemberMap));
    }

    @GetMapping("/list")
    @Operation(summary = "获得代理人分销用户列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:query')")
    public CommonResult<List<AgentBrokerageUserRespVO>> getBrokerageUserList(@RequestParam("ids") Collection<Long> ids) {
        List<AgentBrokerageUserDO> list = brokerageUserService.getBrokerageUserList(ids);
        return success(AgentBrokerageUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理人分销用户分页")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:query')")
    public CommonResult<PageResult<AgentBrokerageUserRespVO>> getBrokerageUserPage(@Valid AgentBrokerageUserPageReqVO pageVO) {
        PageResult<AgentBrokerageUserDO> pageResult = brokerageUserService.getBrokerageUserPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), AgentBrokerageUserDO::getUserId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        Set<Long> agentMemberIds = convertSet(pageResult.getList(), AgentBrokerageUserDO::getAgentUserId);
        Map<Long, MemberUserRespDTO> agentMemberMap = memberUserApi.getUserMap(agentMemberIds);

        return success(AgentBrokerageUserConvert.INSTANCE.convertPage(pageResult, memberMap, agentMemberMap));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理人分销用户 Excel")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-user:export')")
    @OperateLog(type = EXPORT)
    public void exportBrokerageUserExcel(@Valid AgentBrokerageUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AgentBrokerageUserDO> list = brokerageUserService.getBrokerageUserList(exportReqVO);
        // 导出 Excel
        List<AgentBrokerageUserExcelVO> datas = AgentBrokerageUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "代理人分销用户.xls", "数据", AgentBrokerageUserExcelVO.class, datas);
    }

}
